package com.sgb.kjwl.view.ui.activity.power;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;

import com.google.android.material.tabs.TabLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPowerBinding;
import com.sgb.kjwl.di.power.DaggerPowerComponent;
import com.sgb.kjwl.di.power.PowerModule;
import com.sgb.kjwl.model.entity.PowerAchievedEntity;
import com.sgb.kjwl.model.entity.PowerDetailEntity;
import com.sgb.kjwl.model.entity.PowerTalentEntity;
import com.sgb.kjwl.view.ui.adapter.power.PowerAchievedAdapter;
import com.sgb.kjwl.view.ui.adapter.power.PowerQualifyAdapter;
import com.sgb.kjwl.view.ui.adapter.power.PowerTalentAdapter;
import com.sgb.kjwl.viewmodel.power.PowerDetailModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.DialogUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

public class PowerActivity extends AppActivity {

    private ActivityPowerBinding binding;
    @Inject
    PowerDetailModel viewModel;
    private PowerTalentAdapter talentAdapter;
    private PowerAchievedAdapter achievedAdapter;
    private PowerQualifyAdapter adapter;
    private PowerDetailEntity data;
    private List<PowerAchievedEntity> achievedList;
    private List<PowerTalentEntity> talentList;

    public static void start(Context context, String id) {
        Intent intent = new Intent();
        intent.putExtra("id", id);
        intent.setClass(context, PowerActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPowerComponent.builder().appComponent(appComponent).powerModule(new PowerModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_power);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("企业详情");
        binding.tvCallContact.setOnClickListener(this);
        binding.tvCallSgb.setOnClickListener(this);
    }

    @Override
    protected void init() {
        String id = getIntent().getStringExtra("id");
        viewModel.getDetail(id);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_call_contact:
                DialogUtil.showTelDialog(this, data.getLegal(), data.getPhone());
                break;
            case R.id.tv_call_sgb:
                DialogUtil.showTelDialog(this, "客服电话", "4000010051");
                break;
        }
    }

    public void showDetail(PowerDetailEntity data) {
        this.data = data;

        binding.tvName.setText(data.getName());
        //binding.tvLegal.setText("联系人：" + data.getLegal());
        binding.tvPhone.setText("联系人：" + data.getPhone());
        binding.tvLocate.setText("地址：" + data.getCompleteaddress());
        binding.phone3.setText(data.getPhone());
        binding.phone3.setOnClickListener(this);
        binding.local3.setText(data.getCompleteaddress());
        binding.tvCode.setText(data.getCreditCode());
        binding.tvType.setText(data.getCompanyType());
        binding.tvDepute.setText(data.getLegal());
        binding.tvRegLocate.setText(data.getCapital());
        binding.tvPutOnRecordUnit.setText(data.getRecordEnters());
        binding.tvDescribe.setText(data.getIntroduce());

        TabLayout tab = binding.tab;
        tab.addTab(tab.newTab().setText("企业信息"));


        String achi = data.getAchivenums() != null ? data.getAchivenums() : "0";
        String person = data.getPersonums() != null ? data.getPersonums() : "0";
        tab.addTab(tab.newTab().setText("工程业绩" + "(" + achi + ")"));
        tab.addTab(tab.newTab().setText("备案人才" + "(" + person + ")"));

        binding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (position == 0) {
                    binding.svContent.setVisibility(View.VISIBLE);
                    binding.flAchieve.setVisibility(View.GONE);
                    binding.flTalent.setVisibility(View.GONE);

                } else if (position == 1) {
                    binding.svContent.setVisibility(View.GONE);
                    binding.flAchieve.setVisibility(View.VISIBLE);
                    binding.flTalent.setVisibility(View.GONE);

                    if (achievedList == null) {
                        viewModel.getAchieved(data.getNo(), false);
                    }

                } else if (position == 2) {
                    binding.svContent.setVisibility(View.GONE);
                    binding.flAchieve.setVisibility(View.GONE);
                    binding.flTalent.setVisibility(View.VISIBLE);

                    if (talentList == null) {
                        viewModel.getTalent(data.getNo(), false);
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        binding.tvQualify.setText("资质证书(" + data.getQualifys().size() + ")");

        adapter = new PowerQualifyAdapter(this, null);
        binding.rvQualify.setLayoutManager(new LinearLayoutManager(this));
        binding.rvQualify.setAdapter(adapter);
        adapter.setDatas(data.getQualifys());

        binding.tvDescribe.setText(data.getIntroduce());


        achievedAdapter = new PowerAchievedAdapter(this, null);
        binding.rvAchieveList.setLayoutManager(new LinearLayoutManager(this));
        binding.rvAchieveList.setAdapter(achievedAdapter);
        binding.rvAchieveList.setLoadingMoreEnabled(false);


        talentAdapter = new PowerTalentAdapter(this, null);
        binding.rvTalentList.setLayoutManager(new LinearLayoutManager(this));
        binding.rvTalentList.setAdapter(talentAdapter);
        binding.rvTalentList.setLoadingMoreEnabled(false);


        binding.rvAchieveList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                binding.rvAchieveList.setLoadingMoreEnabled(true);
                viewModel.getAchieved(data.getNo(), false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getAchieved(data.getNo(), true);
            }
        });

        binding.rvTalentList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                binding.rvTalentList.setLoadingMoreEnabled(true);
                viewModel.getTalent(data.getNo(), false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getTalent(data.getNo(), true);
            }
        });
    }

    public void showAchieved(List<PowerAchievedEntity> list) {
        achievedList = list;
        if (list.size() == 0) {
            binding.emptyAchieve.setVisibility(View.VISIBLE);
        } else {
            achievedAdapter.setDatas(list);
            binding.rvAchieveList.refreshComplete();
            binding.emptyAchieve.setVisibility(View.GONE);
        }
    }

    public void showTalent(List<PowerTalentEntity> list) {
        talentList = list;
        if (list.size() == 0) {
            binding.emptyTalent.setVisibility(View.VISIBLE);
        } else {
            talentAdapter.setDatas(list);
            binding.rvTalentList.refreshComplete();
            binding.emptyTalent.setVisibility(View.GONE);
        }
    }

    public void showMoreAchieved(List<PowerAchievedEntity> list) {
        if (list.size() == 0) {
            binding.rvAchieveList.setLoadingMoreEnabled(false);
        } else {
            achievedAdapter.addDatas(list);
            binding.rvAchieveList.loadMoreComplete();
        }
    }


    public void showMoreTalent(List<PowerTalentEntity> list) {
        if (list.size() == 0) {
            binding.rvTalentList.setLoadingMoreEnabled(false);
        } else {
            talentAdapter.addDatas(list);
            binding.rvTalentList.loadMoreComplete();
        }
    }
}
